package org.csu.equipment.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.csu.equipment.entity.*;
import org.csu.equipment.service.ClientService;
import org.csu.equipment.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;
    @Autowired
    ClientService clientService;

    @GetMapping("/employeelogin")
    public String login(String CId, String CPwd, Model model){
        int r=employeeService.rank(CId);
        model.addAttribute("rank",r);
        Integer e=employeeService.login(CId,CPwd);
        if (e==1){
            List<Task> taskList=employeeService.getTasksToSelect(CId);
            int s=taskList.size();

//            for (int i=0;i<s;i++){
//                Task task=taskList.get(i);
//                model.addAttribute("task"+i, task);
//            }
            model.addAttribute("tasklist",taskList);
            model.addAttribute("size",s);
            model.addAttribute("employee",employeeService.getEmployee(CId));
            model.addAttribute("employeelogin",employeeService.getEmployeeLogin(CId));
            EmployeeSkill employeeSkill=employeeService.getEmployeeSkill(CId);
            String skill="";
            if (employeeSkill.getAirController()){
                skill=skill+",air_controller";
            }
            if (employeeSkill.getComputer()){
                skill=skill+",computer";
            }
            if (employeeSkill.getElevator()){
                skill=skill+",elevator";
            }
            if (employeeSkill.getLight()){
                skill=skill+",light";
            }
            if (employeeSkill.getPrinter()){
                skill=skill+",printer";
            }
            model.addAttribute("skill",skill);

            List<Task> taskList1= employeeService.getMyTasks(CId);
            model.addAttribute("tasklist1",taskList1);
            return "employee/employee";
        }
        model.addAttribute("msg","账户密码不正确");
        return "/index";
    }

    @PostMapping("/jidan")
    @ResponseBody
    public String jiedan(String TId,String EId,String material){
        String s=employeeService.getTask(TId,EId,material);
        return s;
    }

    @PostMapping("/jutixinx")
    @ResponseBody
    public String jutixinx(String TId){
        Task task=employeeService.getTaskbyId(TId);
        System.out.println(TId);
        System.out.println(task.getTId());
        Client client=clientService.getClient(task.getCId());
        JSONObject json1= (JSONObject) JSON.toJSON(task);
        JSONObject json2= (JSONObject) JSON.toJSON(client);
        JSONObject json3=new JSONObject();
        json3.put("task",json1);
        json3.put("client",json2);
        String json=json3.toString();
        System.out.println(json);
        return json;
    }
    @PostMapping("/updateE")
    @ResponseBody
    public  String updateE(String EName,String EId,String EPhone,String skill,String EPwd){
        Employee employee=employeeService.getEmployee(EId);
        employee.setEName(EName);
        employee.setEPhone(EPhone);
        EmployeeSkill employeeSkill=employeeService.getEmployeeSkill(EId);
        employeeSkill.setAirController(false);
        employeeSkill.setLight(false);
        employeeSkill.setComputer(false);
        employeeSkill.setPrinter(false);
        employeeSkill.setElevator(false);
        String[] skills=skill.split(",");
        for (int i=0;i<skills.length;i++) {
            if (skills[i].equals("elevator")) {
                employeeSkill.setElevator(true);
            }
            if (skills[i].equals("printer")) {
                employeeSkill.setPrinter(true);
            }
            if (skills[i].equals("computer")) {
                employeeSkill.setComputer(true);
            }
            if (skills[i].equals("light")) {
                employeeSkill.setLight(true);
            }
            if (skills[i].equals("air")) {
                employeeSkill.setAirController(true);
            }
        }
        EmployeeLogin employeeLogin=employeeService.getEmployeeLogin(EId);
        employeeLogin.setEPwd(EPwd);
        employeeService.updateEmployee(employee);
        employeeService.updateEmployeeLogin(employeeLogin);
        employeeService.updateEmployeeSkill(employeeSkill);
        return "SUCCESS";
    }
    @PostMapping("/finishtask")
    @ResponseBody
    public void finishtask(String TId,String EId){
        employeeService.finishTask(TId,EId);
    }

    @GetMapping("/yjfk")
    public String yjfk(){
        return "employee/contact";
    }
}
